cd ~/Downloads

# $1 = *.apk
echo $1

PATH="/Users/zhoujie/Library/Android/sdk/build-tools/29.0.2:$PATH"

function sign_apk () {
    # Exception: Invalid architecture `arm` set. Valid options are: armeabi, armeabi-v7a, arm64, arm64-v8a, x86, x86_64
    # $1 = *.apk
    echo "签名: " $1
    apksigner sign --ks ~/.keystore "$1"
}


function embed_gadget () {
    echo "植入gadget: " $1    
    objection patchapk -2 -a arm64-v8a -s "$1"
    # objection patchapk -2 -a armeabi-v7a -s "$1"
}

embed_gadget $1

exit 

# ${var%.apk}
# 该命令的使用是去掉变量var从右边算起的第一个'.'字符及其右边的内容，
# 返回从右边算起的第一个'.'（不含该字符）的左边的内容
# 比如：com.jm.video-刷宝短视频.apk -> com.jm.video-刷宝短视频 
APP_DIR="${1%.apk}"

mkdir ${APP_DIR} 

unzip $1 -d ${APP_DIR}

# exit

# 删除 META-INF 目录
rm -rf ${APP_DIR}/META-INF

# 打包成Apk
cd ${APP_DIR}

zip -r ${APP_DIR}.apk *


# 签名
# apksigner sign --ks ~/.keystore ~/Downloads/${APP_DIR}/${APP_DIR}.apk
sign_apk ~/Downloads/${APP_DIR}/${APP_DIR}.apk 

